package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeSoltVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 预约信息Service业务层处理
 *
 * @author Katana
 * @date 2025-05-23
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    
    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return getById(id);
    }
    
    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        return reservationMapper.selectReservationList(reservation);
    }
    
    
    
    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return updateById(reservation) ? 1 : 0;
    }
    
    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }
    
    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return removeById(id) ? 1 : 0;
    }
    
    /**
     * 查询取消预约数量
     *
     * @param startTime
     * @param endTime
     * @param userId
     * @return
     */
    @Override
    public Integer cancelledCount(LocalDateTime startTime, LocalDateTime endTime, Long userId) {
        return reservationMapper.cancelledCount(startTime, endTime, userId);
    }
    
    /**
     * 查询预约时间段内预约数量
     *
     * @param
     * @return
     */
    @Override
    public List<TimeSoltVo>  countByTime(LocalDateTime startTime) {
        LocalDateTime endTime = startTime.plusDays(1);
        List<TimeSoltVo> timeSoltVos = reservationMapper.countByTime(startTime, endTime);
        return timeSoltVos;
    }
    
    
    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReservation(Reservation reservation) {
        long time = System.currentTimeMillis();
        LocalDateTime now = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = now.toLocalDate().atStartOfDay();
        
        // 查出当天内时间段的预约信息
        List<TimeSoltVo> timeSoltVos = countByTime(startTime);
        // 判断当前时间段预约次数是否为0
        for (TimeSoltVo timeSoltVo : timeSoltVos) {
            if (timeSoltVo.getTime().equals(reservation.getTime())) {
                if (timeSoltVo.getCount() == 0) {
                    throw new RuntimeException("该时间段已经预约满");
                }
            }
        }
        // 设置预约状态为未报到,补充一些基础字段
        reservation.setStatus(0);
        reservation.setCreateTime(now);
        reservation.setCreateBy(UserThreadLocal.getUserId().toString());
        //  保存预约信息
        return save(reservation) ? 1 : 0;
        
    }
    
    
    /**
     * 取消预约
     *
     * @return
     */
    @Override
    public int cancelReservation(Long id) {
        //1 查询该用户当天取消预约的次数,大于三次时抛出异常
        long time = System.currentTimeMillis();
        LocalDateTime now = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = now.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long userId = UserThreadLocal.getUserId();
        Integer count = cancelledCount(startTime, endTime, userId);
        if (count >= 3) {
            throw new RuntimeException("用户当天取消预约次数已达上限");
        }
        
        //2 更改预约状态为取消
        Reservation reservation = new Reservation();
        reservation.setId(id);
        reservation.setUpdateBy(userId.toString());
        reservation.setUpdateTime(now);
        reservation.setStatus(2);
        return updateReservation(reservation);
    }
}
